﻿using BYUStock.Comman;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;

namespace BYUStock
{
    public static class Simulator
    {
        public static CandleCollection MakeCandles(DateTime start, CandleSpan span, int count)
        {
            var seed = new Random();
            var rdm = new Random((int)(seed.NextDouble() * 100));
            var timespan = CandleParser.GetCandleTiemSpan(span);
            var candles = new CandleCollection(startTime: start, span: span);
            var isRising = rdm.Next(100) % 2 == 0;
            var period = rdm.Next(10);
            var tick = 0;
            var MIN = 50;
            var MAX = 100;
            var lastClose = MIN;
            for (int i = 0; i < count; i++)
            {
                if (++tick == period)
                {
                    period = rdm.Next(10);
                    tick = 0;
                    isRising = !isRising;
                }
                int low;
                int high;
                int open;
                int close;
                var currMax = (int)(lastClose * 1.1f);
                var currMin = (int)(lastClose * 0.9f);
                if (isRising)
                {
                    close = rdm.Next(lastClose, currMax);
                    open = rdm.Next(lastClose, currMax);
                }
                else
                {
                    close = rdm.Next(currMin, lastClose);
                    open = rdm.Next(currMin, lastClose);
                }
                var greater = Math.Max(open, close);
                var smaller = Math.Min(open, close);
                high = rdm.Next(greater, currMax);
                low = rdm.Next(currMin, smaller);
                lastClose = close;
                var candle = new Candle()
                {
                    Open = open,
                    High = high,
                    Low = low,
                    Close = close,
                    Start = start = start.Add(timespan),
                    Span = timespan
                };
                candles.Add(candle);
            }
            return candles;
        }
    }
}
